//
//  XLOAuthViewController.swift
//  XLWeiBo
//
//  Created by 李晓龙 on 16/9/20.
//  Copyright © 2016年 lixiaolong. All rights reserved.
/*
 //第三方登陆视图控制器
 */

import UIKit
import SVProgressHUD

//新浪开发平台提供的APPKey
let WeiboAppKey = "2707543009"
//  授权回调页
let WeiboRedirect_Uri = "http://www.itcast.cn"

class XLOAuthViewController: UIViewController {

    //懒加载
    private lazy var webView: UIWebView = UIWebView()
    
    //重写loadView
    override func loadView() {
        
        //解决底部webview黑条
        webView.isOpaque = false
        
        view = webView
        
        webView.delegate = self
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //调用第三方登陆方法
        requestLogin()
        //调用导航栏按钮方法
        setupNavUI()
        
        
    }
    //请求第三方登陆
    private func requestLogin() {
        
        //准备URL 地址
        let url = "https://api.weibo.com/oauth2/authorize?client_id=\(WeiboAppKey)&redirect_uri=\(WeiboRedirect_Uri)"
        
        //创建urlRequest对象
        let urlRequest = URLRequest(url: URL(string: url)!)
        
        webView.loadRequest(urlRequest)
        
    }
    
    //设置导航栏按钮
    private func setupNavUI() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", target: self, action: #selector(cancelAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", target: self, action: #selector(autoFillAction))
        
    }

    //MARK: -- 导航栏按钮点击事件处理
    @objc private func cancelAction() {
        //取消按钮的操作modal
        dismiss(animated: true, completion: nil)
        
    }
    
    @objc private func autoFillAction() {
        
        print("自动填充")
        webView.stringByEvaluatingJavaScript(from: "document.getElementById('userId').value = 'hao123guohaibin@163.com';document.getElementById('passwd').value = 'guohaibin123'")
        
    }
    
    
   
}
// UIWebViewDelegate
extension XLOAuthViewController: UIWebViewDelegate {
 
    //开始加载请求
    func webViewDidStartLoad(_ webView: UIWebView) {
        SVProgressHUD .show()
    }
    //加载完成
    func webViewDidFinishLoad(_ webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    //加载失败
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        SVProgressHUD.dismiss()
    }
    
    //将要加载请求
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        //判断URL 是否合法
        guard let url = request.url else {
            print("url 为 url")
            return false
        }
        
        print(url.absoluteString)
        
        if !url.absoluteString.hasPrefix(WeiboRedirect_Uri) {
            
            //表示不是我们关心
            return true
        }
        
        //代码执行到此表示使我们关心的
        if  let query = url.query ,query.hasPrefix("code ="){  //取到地址栏中的参数 ? 后面的
        
//            print(query)
            
            //根据光标的结束位置获取字符串
            let code = query.substring(from: "code=".endIndex)
            print(code)
            //通过code获取accesstoken
            
            
        }
        

        
        return false
        
    }
    
    
}
































